package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.Goods;
import org.boot.ssm.wxshop.domain.ShopCar;
import org.boot.ssm.wxshop.mapper.GoodsMapper;
import org.boot.ssm.wxshop.service.ShopCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: wxshop
 * @description: 购物车服务层实现
 * @author: Parasomnia
 * @create: 2018-03-16 00:41
 **/
@Service
public class ShopCarServiceImpl extends BaseServiceImpl<ShopCar> implements ShopCarService{

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    @Transactional
    public void addCar(ShopCar shopCar,Integer userId) {
        shopCar.setUserId(userId);
        Integer count = queryCount(shopCar);
        if(count>0){//修改
            shopCar = queryByParam(shopCar);
            shopCar.setNum(shopCar.getNum()+1);
            shopCar.setCountMoney(shopCar.getPrice()*shopCar.getNum());
            updateNoNull(shopCar);
        }else{//添加
            Goods g = goodsMapper.queryByIdPlus(shopCar.getGoodsId());
            shopCar.setPrice(g.getPrice());
            shopCar.setNum(1);
            shopCar.setCountMoney(g.getPrice());
            shopCar.setTitle(g.getTitle());
            shopCar.setGoodsImg(g.getCoverimg());
            saveNoNull(shopCar);
        }
    }
}
